/**
 * 
 */
package wfinder;

import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jlambda.functions.Fun2;

public class Ranking extends Fun2<Pattern, Iterable<String>, Map<String, Integer> > {
	@Override
	public Map<String, Integer> apply(Pattern pattern, Iterable<String> lines) {
		TreeMap<String, Integer> counts = new TreeMap<String, Integer>();

		Matcher matcher = pattern.matcher("");
		// no point in filtering as we actually don't want the lines themselves. doing
		// the regex twice is a perf killer
//		long count = 0;
		for (String string : lines) {
//			count++;
			matcher.reset(string);
			if (matcher.find()) {
				String match = matcher.group(0);
				Integer i = counts.get( match );
				if (i == null) {
					i = 0;
				}
				counts.put(match, i + 1);
			}
		}
//		System.out.println("got "+count);
				
		return counts;
	}
}